<?php
namespace Home\Model;
use Think\Model;

class RolePermissionModel extends Model
{
	//规定表名
	protected $trueTableName = 'role_permission';

	public function get_permiss($role_id,$idOnly=false)
	{
		// var_dump($role_id);exit(); //3
		$ids = [];
		if($role_id){
			$res = $this->where(array("role_id"=>$role_id))->select();
			$ids = [];
			foreach ($res as $key => $value) {
				$ids[] = $value["permission_id"];
			}
		}
		if($idOnly){
			return $ids;
		}
		// dump($ids);exit;
		$res1 = D("permission")->get_permisses($ids,true);
		// dump($res1);exit;
		return $res1;
	}

	public function add_role($last_id,$ids)
	{
		$data['role_id'] = $last_id;
		foreach ($ids as $key => $value) {
			$data['permission_id'] = $value;
			$res = $this->add($data);
		}
		return $res?true:false;
	}

	public function delete_role($id)
	{
		if($id){
			$res = $this->where(array("role_id"=>$id))->delete();
		}
		return $res?true:false;
	}	

	public function edit_role($id,$ids)
	{	
		if($ids){
			$res = $this->delete_role($id);
			if($res){
				$res1 = $this->add_role($id,$ids);
			}
			return $res1?true:false;
		}
	}
}